<template>
  <div class="app-container">
    <el-button
      class="filter-item marginRB10"
      icon="el-icon-plus"
      type="primary"
      @click="handleCreate()"
    >添加
    </el-button
    >
    <el-button class="marginRB10" type="danger" @click="allDel()"
    >批量删除
    </el-button
    >

    <el-table
      ref="dataList"
      :data="menuList"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      border
      row-key="id"
      style="margin-bottom: 20px"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="id" prop="id"></el-table-column>
      <el-table-column label="等级" prop="level"></el-table-column>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="贡献值" prop="value"></el-table-column>
      <el-table-column label="图片" prop="icon">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover" width="100">
            <img
              :src="scope.row.icon"
              alt=""
              style="max-width: 400px; max-height: 400px"
            />
            <img
              slot="reference"
              :src="scope.row.icon"
              alt=""
              style="width: 50px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            plain
            size="mini"
            type="primary"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button
          >
          <cus-del-btn @ok="handleDelete(scope.row)"></cus-del-btn>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :model="form"
        :rules="rules"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto"
      >
        <el-form-item label="等级" prop="level">
          <el-input v-model="form.level" placeholder="请输入等级"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="贡献值" prop="value">
          <el-input v-model="form.value" placeholder="请输入贡献值"/>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleAvatarSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"
          >
            <img v-if="form.icon" :src="form.icon"/>
            <el-button v-else type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteData, getData, postData, postEditData } from '@/api/room/roomVip'

export default {
  data() {
    return {

      confirmBtn: false,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10
      },
      menuList: [],
      dialogFormVisible: false,
      dialogStatus: '',
      titleMap: {
        update: '编辑',
        create: '增加'
      },
      form: {
        id: undefined,
        level: undefined,
        name: undefined,
        icon: undefined,
        value: undefined
      },
      rules: {
        level: [{ required: true, message: '请输入等级', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        value: [{ required: true, message: '请输入贡献值', trigger: 'blur' }],
        icon: [{ required: true, message: '请输入上传图标', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleAvatarSuccess() {
      this.form.icon = this.ossLastImgUrl
    },
    getList() {
      this.listLoading = true
      getData().then(response => {
        this.menuList = response.data
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    handleCreate(row) {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      for (let key in this.form) {
        this.form[key] = row[key]
      }

      this.dialogStatus = 'update'
      this.dialogFormVisible = true

      // this.form = Object.assign({}, this.form)
    },
    handleDelete(row) {
      if (this.confirmBtn) return
      this.confirmBtn = true
      let data = { ids: row.id }
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    allDel() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      let delNum = []
      this.$refs.dataList.selection.forEach(k => {
        delNum.push(k.id)
      })
      if (delNum.length === 0) {
        return
      }
      let data = { ids: delNum.toString() }
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    submitForm() {
      if (this.confirmBtn) return
      this.$refs['dataForm'].validate(valid => {
        if (valid && this.dialogStatus === 'create') {
          this.confirmBtn = true

          postData(this.form).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
              this.dialogFormVisible = false
            } else {
              this.confirmBtn = false
            }
          })
        } else if (valid && this.dialogStatus === 'update') {
          this.confirmBtn = true
          postEditData(this.form).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
              this.dialogFormVisible = false
            } else {
              this.confirmBtn = false
            }
          })
        }
      })
    },
    resetForm() {
      this.form = {
        id: undefined,
        level: undefined,
        name: undefined,
        icon: undefined,
        value: undefined
      }
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
        this.resetForm()
      }
    }
  }
}
</script>
